package belf.migrate.api.taskconf;

import belf.migrate.api.taskconf.typemapping.DataTypeMapping;
import belf.migrate.api.taskconf.typemapping.FieldTypeMapping;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;

/**
 * 迁移任务的配置信息。注意，多个任务可能拥有相同的配置，及TaskConf:Task是N：1
 */
@Data
@NoArgsConstructor
public class TaskConf {
    @JsonProperty("id")
    private long Id;               //主键，在数据库表"migration_task"中被引用为外键
    @JsonProperty("task_name")
    private String taskName;
    @JsonProperty("task_type")
    private String taskType;         //一个描述字符串，用于说明源和目标端的数据库类型，比如“MYSQL-DAMENG”
    @JsonProperty("task_desc")
    private String taskDesc;
    @JsonProperty("source")
    private SourceConf sourceConf;   //源端的配置信息
    @JsonProperty("sink")
    private SinkConf sinkConf;       //目标端的配置信息
    @JsonProperty("mapping")
    private Map<String, List<ColumnSyncConf>> mapping;  // 字段规则映射, table name -> List<ColumnSyncConf>
    @JsonProperty("cdc")
    private Map<String, String> cdc; //数据同步时debezium需要的额外参数
    @JsonProperty("jdbc")
    private List<TableSyncConf> jdbc;  //数据同步时jdbc需要的额外参数

    private String owner;        //任务所属用户。满足多租户管理需求
    private Date createDate;     //创建时间
    @JsonProperty("data_type_mappings")
    private List<DataTypeMapping> dataTypeMappings = new ArrayList<>(); //库一级的全局的数据类型映射，仅在创建表时使用到
    @JsonProperty("table_type_mappings")
    private Map<String, List<FieldTypeMapping>> tableTypeMappings = new HashMap<>();  // 表一级的字段映射规则，key为表名称
    private String aiModel = "Doubao-lite-32k";  //默认使用豆包模型Doubao-pro-32k
    @JsonProperty("evaluate")
    private EvaluateConf evaluateConf;

    public TaskConf(long id, String taskName, String taskType, String taskDesc, SourceConf sourceConf,
                    SinkConf sinkConf, Map<String, List<ColumnSyncConf>> mapping, Map<String, String> cdc,
                    List<TableSyncConf> jdbc, String owner, Date createDate) {
        Id = id;
        this.taskName = taskName;
        this.taskType = taskType;
        this.taskDesc = taskDesc;
        this.sourceConf = sourceConf;
        this.sinkConf = sinkConf;
        this.mapping = mapping;
        this.cdc = cdc;
        this.jdbc = jdbc;
        this.owner = owner;
        this.createDate = createDate;
        dataTypeMappings = new ArrayList<>();
        tableTypeMappings = new HashMap<>();
    }

    public TaskConf(long id, String taskName, String taskType, String taskDesc, SourceConf sourceConf,
                    SinkConf sinkConf, Map<String, List<ColumnSyncConf>> mapping, Map<String, String> cdc,
                    List<TableSyncConf> jdbc, String owner, Date createDate,
                    List<DataTypeMapping> dataTypeMappings, Map<String, List<FieldTypeMapping>> tableTypeMappings) {
        Id = id;
        this.taskName = taskName;
        this.taskType = taskType;
        this.taskDesc = taskDesc;
        this.sourceConf = sourceConf;
        this.sinkConf = sinkConf;
        this.mapping = mapping;
        this.cdc = cdc;
        this.jdbc = jdbc;
        this.owner = owner;
        this.createDate = createDate;
        this.dataTypeMappings = dataTypeMappings;
        if (this.dataTypeMappings == null) {
            this.dataTypeMappings = new ArrayList<>();
        }
        this.tableTypeMappings = tableTypeMappings;
        if (this.tableTypeMappings == null) {
            this.tableTypeMappings = new HashMap<>();
        }
    }
}
/* MySQL中的建表语句
CREATE TABLE migration_task_conf (
    id INT AUTO_INCREMENT PRIMARY KEY,
    task_name VARCHAR(128),
    task_type VARCHAR(128),
    task_desc VARCHAR(255),
    source JSON,
    sink JSON,
    cdc JSON,
    owner VARCHAR(64),
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
 */